var express = require('express');
var router = express.Router();
const {formidable} = require('formidable') ;
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/upload',function(req,res,next){
  const form = formidable({
    multiples: true,
    // 设置上传文件的保存目录
    uploadDir: __dirname + '/../public/images',
    // 保留文件后缀
    keepExtensions: true
  });
  
  form.parse(req, (err, fields, files) => {
    if (err) {
      next(err);
      return;
    }
    console.log('传值',fields)
    let url = '/images/' + files.file[0].newFilename;
    res.json({
      ...fields,
      image:url
    })
  });
})

module.exports = router;
